/**
 * tool package
 * For not using jquery methods.
 * author：splash
 * date：2018、4、18
 */

//dom class operation
class domClass {
    hasClass(e: any, name: string): boolean {
        return (new RegExp('(\\s|^)' + name + '(\\s|$)')).test(e.className);
    }
    addClass(e: any, name: string) {
        if (!this.hasClass(e, name)) {
            e.className = e.className.replace(/^\s+|\s+$/g, '');
            e.className = `${e.className} ${name}`;
        }
    }
    removeClass(e: any, name: string) {
        if (this.hasClass(e, name)) {
            let reg: any = new RegExp('(\\s|^)' + name + '(\\s|$)');
            e.className = e.className.replace(reg, '');
        }
    }
}

//cookit operation
class cookies {
    private cookies: string[] = document.cookie.replace(/\s/g, '').split(';');
    private tmp: string[];
    private price: string = '';
    getCookie(name: string): string {
        this.cookies.forEach(e => {
            if (this.price !== '') return;
            this.tmp = e.split('=');
            this.price = this.tmp[0] === name ? this.tmp[1] : '';
        }, this);
        return this.price;
    }
    setCookie(name: string, v: any, d: any, domain: string = '') {
        let date = new Date();
        date.setDate(date.getDate() + d);
        document.cookie = domain === '' ? `${name}=${v};expires=${date}` : `${name}=${v};expires=${date};domain=${domain}`;
    }

    removeCookie(name: string) {
        this.setCookie(name, '1', -1);
    }
}

//keyword value operation
const keyCodeMap = {
    8: 'Backspace',
    9: 'Tab',
    13: 'Enter',
    16: 'Shift',
    17: 'Ctrl',
    18: 'Alt',
    19: 'Pause',
    20: 'Caps Lock',
    27: 'Escape',
    32: 'Space',
    33: 'Page Up',
    34: 'Page Down',
    35: 'End',
    36: 'Home',
    37: 'Left',
    38: 'Up',
    39: 'Right',
    40: 'Down',
    42: 'Print Screen',
    45: 'Insert',
    46: 'Delete',
    48: '0',
    49: '1',
    50: '2',
    51: '3',
    52: '4',
    53: '5',
    54: '6',
    55: '7',
    56: '8',
    57: '9',
    65: 'A',
    66: 'B',
    67: 'C',
    68: 'D',
    69: 'E',
    70: 'F',
    71: 'G',
    72: 'H',
    73: 'I',
    74: 'J',
    75: 'K',
    76: 'L',
    77: 'M',
    78: 'N',
    79: 'O',
    80: 'P',
    81: 'Q',
    82: 'R',
    83: 'S',
    84: 'T',
    85: 'U',
    86: 'V',
    87: 'W',
    88: 'X',
    89: 'Y',
    90: 'Z',
    91: 'Windows',
    93: 'Right Click',
    96: 'Numpad 0',
    97: 'Numpad 1',
    98: 'Numpad 2',
    99: 'Numpad 3',
    100: 'Numpad 4',
    101: 'Numpad 5',
    102: 'Numpad 6',
    103: 'Numpad 7',
    104: 'Numpad 8',
    105: 'Numpad 9',
    106: 'Numpad *',
    107: 'Numpad +',
    109: 'Numpad -',
    110: 'Numpad .',
    111: 'Numpad /',
    112: 'F1',
    113: 'F2',
    114: 'F3',
    115: 'F4',
    116: 'F5',
    117: 'F6',
    118: 'F7',
    119: 'F8',
    120: 'F9',
    121: 'F10',
    122: 'F11',
    123: 'F12',
    144: 'Num Lock',
    145: 'Scroll Lock',
    182: 'My Computer',
    183: 'My Calculator',
    186: ';',
    187: '=',
    188: ',',
    189: '-',
    190: '.',
    191: '/',
    192: '`',
    219: '[',
    220: '\\',
    221: ']',
    222: '\''
};

interface browserTxt {
    ie: string;
    edge: string;
    firefox: string;
    opera: string;
    chrome: string;
    safari: string;
}

//browser core operation
let getExplore: () => any = function () {
    let sys: browserTxt = {
        ie: '',
        edge: '',
        firefox: '',
        opera: '',
        chrome: '',
        safari: ''
    };
    let ua: string = navigator.userAgent.toLowerCase();

    (this.s = ua.match(/rv:([\d.]+)\) like gecko/)) ? sys.ie = this.s[1] :
        (this.s = ua.match(/msie ([\d\.]+)/)) ? sys.ie = this.s[1] :
            (this.s = ua.match(/edge\/([\d\.]+)/)) ? sys.edge = this.s[1] :
                (this.s = ua.match(/firefox\/([\d\.]+)/)) ? sys.firefox = this.s[1] :
                    (this.s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? sys.opera = this.s[1] :
                        (this.s = ua.match(/chrome\/([\d\.]+)/)) ? sys.chrome = this.s[1] :
                            (this.s = ua.match(/version\/([\d\.]+).*safari/)) ? sys.safari = this.s[1] : 0;

    if (sys.ie) return ('IE: ' + sys.ie);
    if (sys.edge) return ('EDGE: ' + sys.edge);
    if (sys.firefox) return ('Firefox: ' + sys.firefox);
    if (sys.chrome) return ('Chrome: ' + sys.chrome);
    if (sys.opera) return ('Opera: ' + sys.opera);
    if (sys.safari) return ('Safari: ' + sys.safari);
    return 'unkonw';
};

//get the scrollTop
let getScrollTop = () => {
    return (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
};

/**
 * verify operation
 * Validation email
 * Validation ID card
 * Validation Phone number
 */
class verify {
    isEmail(url: string): boolean {
        return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(url);
    };
    isIdCard(txt: string): boolean {
        return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(txt)
    };
    isPhoneNum(iphoneNo: number | string): boolean {
        if (typeof iphoneNo === 'string') {
            iphoneNo = iphoneNo.toString();
        }
        return /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(<string>iphoneNo);
    }
    isUrl(url: string): boolean {
        return /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i.test(url);
    }
}

// get the url parameter
let parseQueryString = (url: string) => {
    this.url = url == null ? window.location.href : url;
    this.search = this.url.substring(this.url.lastIndexOf('?') + 1);
    if (!this.search) {
        return {}
    }
    this.jsons = `{"${decodeURIComponent(this.search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`;
    return JSON.parse(this.jsons);
};

export { domClass, cookies, keyCodeMap, getExplore, getScrollTop, verify, parseQueryString };